<template>
	<view>
		这是说明书
	</view>
	
<!--    <view>
        <scroll-view
            scroll-y
            class="box"
        >
            <view class="item" v-for="(item, index) in blueDeviceList" :key="index" @click="connect(item)">
                <view>
                    <text>id: {{ item.deviceId }}</text>    
                </view>
                <view>
                    <text>name: {{ item.name }}</text>  
                </view>
            </view>
        </scroll-view>
        
        <button @click="initBlue">1 初始化蓝牙</button>
        
        <button @click="discovery">2 搜索附近蓝牙设备</button>
        
        <button @click="getServices">3 获取蓝牙服务</button>
        
        <button @click="getCharacteristics">4 获取特征值</button>
        
        <button @click="notify">5 开启消息监听</button>
        
        <button @click="send">6 发送数据</button>
        
        <button @click="read">7 读取数据</button>
        
        <view class="msg_x">
            <view class="msg_txt">
                监听到的内容：{{ message }}
            </view>
            <view class="msg_hex">
                监听到的内容（十六进制）：{{ messageHex }}
            </view> 
        </view> 
    </view> -->
</template>

<script>
export default {
    data() {
        return {
            blueDeviceList: [],
            deviceId: '60:E8:5B:6C:26:08',
            serviceId: '0000FFE0-0000-1000-8000-00805F9B34FB',
            characteristicId: '0000FFE1-0000-1000-8000-00805F9B34FB',
            message: '',
            messageHex: ''
        };
    },
    methods: {
        initBlue() {
            uni.openBluetoothAdapter({
                success(res) {
                    console.log('初始化蓝牙成功')
                    console.log(res)
                },
                fail(err) {
                    console.log('初始化蓝牙失败')
                    console.error(err)
                }
            });
        },
        discovery() {
            uni.startBluetoothDevicesDiscovery({
                success: (res) => {
                    console.log('开始搜索')
                    uni.onBluetoothDeviceFound(this.found)
                },
                fail(err) {
                    console.log('搜索失败')
                    console.error(err)
                }
            });
        },
        found(res) {
            //console.log(res);
            this.blueDeviceList.push(res.devices[0]);
        },
        connect(data) {
            console.log(data);
            this.deviceId = data.deviceId;
            uni.createBLEConnection({
                deviceId: this.deviceId,
                success: (res) => {
                    console.log('连接成功');
                    console.log(res);
                    this.stopDiscovery();
                    uni.showToast({
                        title: '连接成功'
                    });
                },
                fail(err) {
                    console.log('连接失败');
                    console.error(err);
                    uni.showToast({
                        title: '连接失败',
                        icon: 'error'
                    });
                }
            });
        },
        stopDiscovery() {
            uni.stopBluetoothDevicesDiscovery({
                success(res) {
                    console.log('停止成功');
                    console.log(res);
                },
                fail(err) {
                    console.log('停止失败');
                    console.error(err);
                }
            });
        },
        getServices() {
            uni.getBLEDeviceServices({
                deviceId: this.deviceId,
                success: (res) => {
                    console.log(res);
                    uni.showToast({
                        title: '获取服务成功'
                    });
                },
                fail(err) {
                    console.error(err);
                    uni.showToast({
                        title: '获取服务失败',
                        icon: 'error'
                    });
                }
            });
        },
        getCharacteristics() {
            uni.getBLEDeviceCharacteristics({
                deviceId: this.deviceId,
                serviceId: this.serviceId,
                success: (res) => {
                    console.log(res);
                    uni.showToast({
                        title: '获取特征值成功'
                    });
                },
                fail(err) {
                    console.error(err);
                    uni.showToast({
                        title: '获取特征值失败',
                        icon: 'error'
                    });
                }
            });
        },
        notify() {
            uni.notifyBLECharacteristicValueChange({
                deviceId: this.deviceId,
                serviceId: this.serviceId,
                characteristicId: this.characteristicId,
				state:true,
                success: (res) => {
                    console.log(res);
                    this.listenValueChange();
                    uni.showToast({
                        title: '已开启监听'
                    });
                },
                fail(err) {
                    console.error(err);
                    uni.showToast({
                        title: '监听失败',
                        icon: 'error'
                    });
                }
            });
        },
        ab2hex(buffer) {
            const hexArr = Array.prototype.map.call(
                new Uint8Array(buffer),
                function (bit) {
                    return ('00' + bit.toString(16)).slice(-2);
                }
            );
            return hexArr.join('');
        },
        hexCharCodeToStr(hexCharCodeStr) {
            var trimedStr = hexCharCodeStr.trim();
            var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
            var len = rawStr.length;
            if (len % 2 !== 0) {
                alert("存在非法字符!");
                return "";
            }
            var curCharCode;
            var resultStr = [];
            for (var i = 0; i < len; i = i + 2) {
                curCharCode = parseInt(rawStr.substr(i, 2), 16);
                resultStr.push(String.fromCharCode(curCharCode));
            }
            return resultStr.join("");
        },
        listenValueChange() {
            uni.onBLECharacteristicValueChange((res) => {
                console.log(res);
                let resHex = this.ab2hex(res.value);
                console.log(resHex);
                this.messageHex = resHex;
                let result = this.hexCharCodeToStr(resHex);
                console.log(String(result));
                this.message = String(result);
            });
        },
        send() {
            let msg = 'hello';
            const buffer = new ArrayBuffer(msg.length);
            const dataView = new DataView(buffer);
            for (var i = 0; i < msg.length; i++) {
                dataView.setUint8(i, msg.charAt(i).charCodeAt());
            }
            uni.writeBLECharacteristicValue({
                deviceId: this.deviceId,
                serviceId: this.serviceId,
                characteristicId: this.characteristicId,
                value: buffer,
                success(res) {
                    console.log('writeBLECharacteristicValue success', res.errMsg);
                    uni.showToast({
                        title: 'write指令发送成功'
                    });
                },
                fail(err) {
                    console.error(err);
                    uni.showToast({
                        title: 'write指令发送失败',
                        icon: 'error'
                    });
                }
            });
        },
        read() {
            uni.readBLECharacteristicValue({
                deviceId: this.deviceId,
                serviceId: this.serviceId,
                characteristicId: this.characteristicId,
                success(res) {
                    console.log(res);
                    uni.showToast({
                        title: 'read指令发送成功'
                    });
                },
                fail(err) {
                    console.error(err);
                    uni.showToast({
                        title: 'read指令发送失败',
                        icon: 'error'
                    });
                }
            });
        }
    }
}
</script>

<style>
.box {
    width: 98%;
    height: 400rpx;
    box-sizing: border-box;
    margin: 0 auto 20rpx;
    border: 2px solid dodgerblue;
}
.item {
    box-sizing: border-box;
    padding: 10rpx;
    border-bottom: 1px solid #ccc;
}
button {
    margin-bottom: 20rpx;
}
.msg_x {
    border: 2px solid seagreen;
    width: 98%;
    margin: 10rpx auto;
    box-sizing: border-box;
    padding: 20rpx;
}
.msg_x .msg_txt {
    margin-bottom: 20rpx;
}
</style>
